package com.zareal.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zareal.domain.Pruduct;
import com.zareal.feign.SystemFeignClient;
import com.zareal.mapper.PruductMapper;
import com.zareal.result.JSONResult;
import com.zareal.service.IPruductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wilson
 * @since 2022-09-28
 */
@Service
public class PruductServiceImpl extends ServiceImpl<PruductMapper, Pruduct> implements IPruductService {
    @Autowired
    private SystemFeignClient systemFeignClient;
    @Override
    public Pruduct selectById(Serializable id) {
        // 父类查询出来产品
        Pruduct pruduct = super.selectById(id);
        // 调用feign接口查询
        Long pruductId = Long.valueOf(pruduct.getPruductType());
        JSONResult jsonResult = systemFeignClient.get(pruductId);
        Object data = jsonResult.getData();
        Map type = (Map) data;
        String pruductType = (String) type.get("pruductType");
        pruduct.setPruductType(pruductType);
        return pruduct;
    }

    @Override
    public List<Pruduct> selectList(Wrapper<Pruduct> wrapper) {
        List<Pruduct> list = super.selectList(wrapper);
        for (Pruduct pruduct : list) {
            // 调用feign接口查询
            Long pruductId = Long.valueOf(pruduct.getPruductType());
            JSONResult jsonResult = systemFeignClient.get(pruductId);
            Object data = jsonResult.getData();
            Map type = (Map) data;
            String pruductType = (String) type.get("pruductType");
            pruduct.setPruductType(pruductType);
        }
        return list;
    }
}
